package com.ruoyi.washer.common;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.washer.mapper.XwDeviceMapper;
import com.ruoyi.washer.mapper.XwProductMapper;
import com.ruoyi.washer.mapper.XwPurchaseOrderMapper;
import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Credentials;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Validator;
import com.wechat.pay.contrib.apache.httpclient.cert.CertificatesManager;
import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.security.PrivateKey;

@Service
public class PlatformToolServiceImpl {
    @Value("${wx.appid}")
    private String appid;

    @Value("${wx.appsecret}")
    private String secret;

    @Value("${wx.mch_id}")
    private String mchId;

    @Value("${wx.key_v2}")
    private String key_v2;

    @Value("${wx.key_v3}")
    private String key_v3;

    @Value("${wx.notify_url}")
    private String notify_url;

    @Value("${wx.refund_notify_url}")
    private String refund_notify_url;

    @Value("${wx.private_key}")
    private String private_key;

    @Value("${wx.mch_serial_no}")
    private String mch_serial_no;

    private static final Logger logger = LoggerFactory.getLogger(PlatformToolServiceImpl.class);



    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private XwDeviceMapper xwDeviceMapper;

    @Autowired
    private XwProductMapper xwProductMapper;

    @Autowired
    private XwPurchaseOrderMapper xwPurchaseOrderMapper;

    /*
   获取平台证书列表
    */
    public JSONObject getPlatformKey() throws Exception{
        JSONObject bodyAsString=null;
        try {
            PrivateKey merchantPrivateKey = PemUtil
                    .loadPrivateKey(new ByteArrayInputStream(private_key.getBytes("utf-8")));
            // 获取证书管理器实例
            CertificatesManager certificatesManager = CertificatesManager.getInstance();
            // 向证书管理器增加需要自动更新平台证书的商户信息
            certificatesManager.putMerchant(mchId,
                    new WechatPay2Credentials(mchId,
                            new PrivateKeySigner(mch_serial_no, merchantPrivateKey)),
                    key_v3.getBytes(StandardCharsets.UTF_8));
            CloseableHttpClient httpClient = WechatPayHttpClientBuilder.create()
                    .withMerchant(mchId, mch_serial_no, merchantPrivateKey)
                    .withValidator(new WechatPay2Validator(certificatesManager.getVerifier(mchId)))
                    .build();
            URIBuilder uriBuilder = new URIBuilder("https://api.mch.weixin.qq.com/v3/certificates");
            HttpGet httpGet = new HttpGet(uriBuilder.build());
            httpGet.addHeader("Accept", "application/json");
            httpGet.addHeader("User-Agent", "用户代理(https://zh.wikipedia.org/wiki/User_agent)");
            CloseableHttpResponse response = httpClient.execute(httpGet);
            String bodyAsString2 = EntityUtils.toString(response.getEntity());
            System.out.println(bodyAsString);
            bodyAsString=JSONObject.parseObject(bodyAsString2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bodyAsString;
    }

}
